package com.entrogy.service.impl;

import com.entrogy.bean.*;
import com.entrogy.dao.RoleMapper;
import com.entrogy.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findRoles(String roleName,int schoolCode) {
        return roleMapper.findRoles(roleName,schoolCode);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateRoles(List<UpdateRoleEntity> updateRoles) {
        //要更新角色的权限信息前，需要提前把该角色的当前权限信息删除，因为，更新角色的权限信息不知道是更新还是有增加或者删除
        for (UpdateRoleEntity updateRole : updateRoles) {
            int roleId = updateRole.getRoleId();
            roleMapper.deleteRolesPermission(roleId);
        }
        for (UpdateRoleEntity updateRole : updateRoles) {
            int roleId = updateRole.getRoleId();
            List<Integer> pIds = updateRole.getPermissionIds();
            roleMapper.updateRole(roleId,pIds);
            //权限信息更新完毕，应该更新菜单信息，首先清空该角色的菜单信息
            roleMapper.deleteRolesMenu(roleId);
            //找到该角色所对应权限的对应菜单
            allocationMenuForRole(roleId);
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int discardRoleByIds(List<Integer> discardRoleIds) {
        for (Integer roleId : discardRoleIds) {
            roleMapper.discardRoleById(roleId);
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int enableRoleByIds(List<Integer> enableRoleIds) {
        for (Integer roleId : enableRoleIds) {
            roleMapper.enableRoleById(roleId);
        }
        return 0;
    }

    @Override
    public List<Role> findAllRolesSimple(Integer schoolCode) {
        List<Role> roles = roleMapper.findAllRolesSimple(schoolCode);
        return roles;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addRole(AddRoleEntity addRole) {

        //新增一个角色首先新增角色表，然后在role_permission表中为角色进行授权
        //返回值为插入到角色表中新增的角色，采用主键回填，用于为新增的角色设置权限
        int result = roleMapper.addRole(addRole);
        int roleId = addRole.getRoleId();
        List<Integer> pIds = addRole.getPermissionIds();
        //插入到权限表中的行数记录，用于判断是否全部的权限已经插入正确
        int pRow = 0;
        for(Integer pId : pIds){
            int row = roleMapper.addRoleAndPermission(roleId,pId);
            if (row == 1){
                pRow++;
            }
        }
        //为已分配权限的角色分配菜单
        allocationMenuForRole(roleId);
        return result > 0 && pRow == pIds.size();
    }

    @Override
    public List<PermissionAndMenu> findAllPermission(int schoolCode) {
        List<PermissionAndMenu> permissionAndMenus = roleMapper.findAllPermission(schoolCode);
        return permissionAndMenus;
    }

    /**
     *  查询管理员获得的所有角色
     * @return
     */
    @Override
    public List<Role> selectAllRoles(){
        return roleMapper.selectAllRoles();
    }

    @Override
    public List<Role> findRolesByUsername(String username) {
        List<Role> role = roleMapper.findRolesByUsername(username);
        return role;
    }

    @Override
    public Role findRoleByRoleId(Integer roleId) {
        return roleMapper.findRoleByRoleId(roleId);
    }

    @Override
    public List<Integer> listHasSomePermissionRole(String permissionCode, Integer schoolCode) {
        return roleMapper.listHasSomePermissionRole(permissionCode, schoolCode);
    }

    //为已分配权限的角色分配菜单
    public void allocationMenuForRole(Integer roleId){
        //找到该角色所对应权限的对应菜单
        List<Integer> menuIds = roleMapper.findPermissionsMenuIds(roleId);
        int result1 = 0;
        int result2 = 0;
        List<Integer> parentMenuIds = new ArrayList<>();
        if (menuIds.size() > 0){
            //插入该角色对应的子菜单
            result1 = roleMapper.insertChildMenus(roleId,menuIds);
            //子菜单插入后，应该将其父菜单信息一并存入
            parentMenuIds = roleMapper.findMenusParentMenuIds(menuIds);
            if (parentMenuIds.size() > 0){
                result2 = roleMapper.insertChildMenus(roleId,parentMenuIds);
            }
        }
        if (result1 != menuIds.size() || result2 != parentMenuIds.size()){
            throw new RuntimeException("更新权限时，菜单出现异常！");
        }
    }

}
